package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.TreeNode;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName BinaryTreeIsSymmetricV1_Bad
 * @Description BinaryTreeIsSymmetricV1_Bad
 * @Author wanghaiwei
 * @Date 2022/11/24 13:49
 */
public class BinaryTreeIsSymmetricV1_Bad {
    public boolean isSymmetric(TreeNode root) {
        List<Integer> leftSearchResult = leftTreeSearch(root.left);
        List<Integer> rightSearchResult = rightTreeSearch(root.right);
        if (leftSearchResult.size() != rightSearchResult.size()) {
            return false;
        }else {
            for (int i = 0; i < leftSearchResult.size(); i++) {
                if (!leftSearchResult.get(i).equals(rightSearchResult.get(i))){
                    return false;
                }
            }
        }
        return true;
    }

    public List<Integer> leftTreeSearch(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) {
            result.add(-9999);
            return result;
        }
        result.addAll(leftTreeSearch(root.right));
        result.addAll(leftTreeSearch(root.left));
        result.add(root.val);
        return result;
    }

    public List<Integer> rightTreeSearch(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        if (root == null) {
            result.add(-9999);
            return result;
        }
        result.addAll(rightTreeSearch(root.left));
        result.addAll(rightTreeSearch(root.right));
        result.add(root.val);
        return result;
    }

    public static void main(String[] args) {
        BinaryTreeIsSymmetricV1_Bad solution = new BinaryTreeIsSymmetricV1_Bad();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.left = new TreeNode(3);
        root.left.right = new TreeNode(4);
        root.right.left = new TreeNode(4);
        root.right.right = new TreeNode(3);
        Assert.isTrue(solution.isSymmetric(root), "[1,2,2,3,4,4,3]结果计算错误");

        TreeNode root2 = new TreeNode(1);
        root2.left = new TreeNode(2);
        root2.left.right = new TreeNode(3);
        root2.right = new TreeNode(2);
        root2.right.right = new TreeNode(3);
        Assert.isTrue(!solution.isSymmetric(root2), "[1,2,2,null,3,null,3]结果计算错误");

        TreeNode root3 = new TreeNode(1);
        root3.left = new TreeNode(2);
        root3.left.left = new TreeNode(2);
        root3.right = new TreeNode(2);
        root3.right.left = new TreeNode(2);
        Assert.isTrue(!solution.isSymmetric(root3), "[1,2,2,2,null,2]结果计算错误");
    }
}
